package com.example.universita.esami;

import android.net.Uri;
import android.os.Bundle;
import android.content.Intent;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ContattaDocente extends Activity {
	
	private boolean flag=false;
	private TextView tv;
	private Button b; 
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contatta_docente);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_contatta_docente, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	Intent intent;
    	
    	switch(item.getItemId()){
    	    case R.id.statistiche: // Vai a Statistiche 
 	    	   		intent = new Intent (this, StatisticActivity.class);
 	    	   		startActivity(intent);
 	    	   		break;  
    	    case R.id.reset: // Resetta EditText
	    	   		ResetValues();
	    	   		break; 
    	    case R.id.dettagli: // Vai a Dettagli Esame
	    	   		intent = new Intent (this, Details.class);
	    	   		startActivity(intent);
	    	   		break;       		
    	    case R.id.carriera: // Vai a Carriera
 	    	   		intent = new Intent(this, Carriera.class);
 	    	   		startActivity(intent);  
 	    	   		break;             
    	    case R.id.home: // Vai alla Home
   	   	   			intent = new Intent(this, MainActivity.class);
   	   	   			startActivity(intent);  
   	   	   			break;  
    	    case R.id.inserisci: //Va al programma per l'invio della mail
	    	   		invio();
	    	   		break; 
    	    case R.id.annulla: // Vai a Dettagli Esame
    	   			finish();
    	   			break;    		
    	}
        return true;
    }
    
    public void ResetValues(){
    	b = (Button) findViewById(R.id.buttoncontatta);
    	tv = (TextView) findViewById(R.id.mail_docente);	
    	
    	b.setText("Selezionare il docente");
    	tv.setText("Selezionare il docente"); 
    	
    }
    
    public void selezionaDocente(View v){
    	
    	
    	int i = 0;
    	DBDataUniversity db = new DBDataUniversity(this);
    	db.open();
    	Cursor c=db.getDocenti();
    	while(c.moveToNext()){
			
			i++;
			
		}
    	
    	c.moveToFirst();
    	
    	final String[] options= new String[i-1];
    	
    	i = 0;
    	
    	while(c.moveToNext()){
			
			options[i]=c.getString(0);
			i++;
			
		}
    	db.close();
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Selezionare il docente.");
		builder.setItems(options, new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialog, int which) {
				String option = options[which];
				tv = (TextView) findViewById(R.id.mail_docente);
				b = (Button) findViewById(R.id.buttoncontatta);
				DBDataUniversity db = new DBDataUniversity(getBaseContext());
		    	db.open();
		    	b.setText(option);
		    	Cursor c = db.getDettagli_Docenti(option);
		    	c.moveToNext();
		    	String mail = c.getString(1);
				tv.setText(mail);
				flag=true;
				db.close();
				
			}
		});
		AlertDialog alert = builder.create();
		alert.show();
		
    }
    
    public void invio(){
    	Toast t;
    	
    	if(flag==true){
	    	Intent emailIntent = new Intent(android.content.Intent.ACTION_SENDTO);
	    	emailIntent.setData(Uri.parse("mailto:"+tv.getText().toString()));
	    	startActivity(emailIntent);
    	}else{	
        	
			t = Toast.makeText(this, "Selezionare l'esame.", Toast.LENGTH_SHORT);
			t.show();
		
    	}	
    }
    
}
